package cn.ekgc.serve.controller;

import cn.ekgc.common.jwt.JwtUtils;
import cn.ekgc.system.pojo.User;
import cn.ekgc.system.pojo.dto.RespBody;
import cn.ekgc.system.pojo.vo.MenuTree;
import cn.ekgc.system.service.MenuService;
import cn.ekgc.system.service.UserService;
import cn.ekgc.system.utils.RedisUtils;
import cn.ekgc.system.pojo.SysUser;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtils redisUtil;

    @Resource
    private MenuService menuService;

    @Autowired
    private JwtUtils jwtUtils;

    @RequestMapping("/test")
    public Object test(@RequestHeader String token) {
        return "test";
    }

    @RequestMapping("/testRedis")
    public String testRedis() {
        redisUtil.putValue("name", "cw");
        redisUtil.putValueByTime("name2", "cw2", 60);
        return "test";
    }

    @RequestMapping("/login")
    public RespBody login(HttpSession session, String username, String password) {
        User u = userService.login(username, password);

        if (null != u) {
            Map<String, User> user = new HashMap<>();
            user.put("user", u);
            String b = jwtUtils.getToken(user);
            session.setAttribute("token", b);
            return new RespBody<>(200, b, "登录成功");
        }
        return new RespBody<>(400, null, "用户名或密码输入不正确");
    }

    @RequestMapping("/logout")
    public RespBody logout(HttpSession session, String username, String password) {

        User u = userService.login(username, password);
        if (null != u) {
            session.setAttribute("token", null);
            return new RespBody<>(200, null, "退出登录成功");
        }
        return new RespBody<>(400, null, "退出登录失败");
    }


    @RequestMapping("/addUser")
    public String addUser(@RequestBody SysUser su) {
        System.out.println(su);
        return "addUser";
    }

    @RequestMapping("/userInfo")
    public RespBody<?> userInfo(@RequestParam String token) {

        Object o = jwtUtils.vertify(token);
        User u = userService.analysisUser(o);
        HashMap<String, Object> data = new HashMap<>();
        //查出用户信息
        data.put("user", u);
        //用户所有的角色
//        data.put("roles", new ArrayList<>());
        //用户所有的权限
//      List<MenuTree> menus=  menuService.getMenuTreeByUserId(1L);
        List<String> permissions = new ArrayList<>();
        data.put("permissions", permissions);
        return new RespBody<>(200, data, null);
    }

    @RequestMapping("/routers")
    public RespBody<?> routers(@RequestParam String token) {
        Object o = jwtUtils.vertify(token);
        User u = userService.analysisUser(o);
        //用户所有的菜单
        List<MenuTree> menus = menuService.getMenuTreeByUserId(u.getId());
        return new RespBody<>(200, menus, null);
    }
}